import {userSeekHelp} from '@/common/index.js'
import apiW from '@/common/indexW.js'
import store from '../store/index.js'
import sharePlun from "@/common/share.js";
export const share = {
	shareData(data){
		uni.setClipboardData({
			data: data,
			success: function () {
				console.log('success');
				uni.showModal({
					title: '复制成功',
					content: '内容已复制到粘贴板，可前往其他应用粘贴查看。', 
					showCancel:false,
					success: function(res) {
						if (res.confirm) {											 
							//console.log('用户点击确定');
						} else if (res.cancel) {
							//console.log('用户点击取消');
						}
					}
				});
			}
		});
	},
	getUrl(){
		let userInfo = uni.getStorageSync('userInfo');
		// let res = uni.getStorageSync('xinliConfig').shareData || 'http://47.113.116.215/xinli/#/pages/login/registration?invitationcode=';
		let res =  'http://47.113.116.215/xinli/#/pages/login/registration?invitationcode=';
		return res + userInfo.invitationcode;
	},
	shareUrl(){
		let data = '发送给同样恐惧的朋友，或发送到qq群贴吧中。@别的朋友。来到我们平台\n' + this.getUrl();
		console.log('url',data);
		// #ifndef H5
		uni.setClipboardData({
			data: data,
			success: function () {
				console.log('success');
				uni.showModal({
					title: '复制成功',
					content: '内容已复制到粘贴板，可前往其他应用粘贴查看。', 
					showCancel:false,
					success: function(res) {
						if (res.confirm) {											 
							//console.log('用户点击确定');
						} else if (res.cancel) {
							//console.log('用户点击取消');
						}
					}
				});
			}
		});
		// #endif
	},
	
	shareApp(){
		let shareInfo={
			href: this.getUrl(),
			title:"战胜恐惧",
			desc:"发送给同样恐惧的朋友，或发送到qq群贴吧中。@别的朋友。来到我们平台",
			imgUrl:""
		};
		let shareList=[
			{
				icon:"/static/sharemenu/wx.png",
				text:"微信好友",
			},
			{
				icon:"/static/sharemenu/pyq.png",
				text:"朋友圈"
			},
			{
				icon:"/static/sharemenu/weibo.png",
				text:"微博"
			},
			{
				icon:"/static/sharemenu/qq.png",
				text:"QQ"
			},
			{
				icon:"/static/sharemenu/copy.png",
				text:"复制"
			},
			{
				icon:"/static/sharemenu/more.png",
				text:"更多"
			},
		];
		let shareObj=sharePlun(shareInfo,shareList,function(index){
			console.log("点击按钮的序号: " + index);
			let shareObj={
				href:shareInfo.href||"",
				title:shareInfo.title||"",
				summary:shareInfo.desc||"",
				success:(res)=>{
					console.log("success:" + JSON.stringify(res));
				},
				fail:(err)=>{
					console.log("fail:" + JSON.stringify(err));
				}
			};
			switch (index) {
				case 0:
					shareObj.provider="weixin";
					shareObj.scene="WXSceneSession";
					shareObj.type=0;
					shareObj.imageUrl=shareInfo.imgUrl||"";
					uni.share(shareObj);
					break;
				case 1:
					shareObj.provider="weixin";
					shareObj.scene="WXSenceTimeline";
					shareObj.type=0;
					shareObj.imageUrl=shareInfo.imgUrl||"";
					uni.share(shareObj);
					break;
				case 2:
					shareObj.provider="sinaweibo";
					shareObj.type=0;
					shareObj.imageUrl=shareInfo.imgUrl||"";
					uni.share(shareObj);
					break;
				case 3:
					shareObj.provider="qq";
					shareObj.type=1;
					shareObj.imageUrl=shareInfo.imgUrl||"";
					uni.share(shareObj);
					break;
				case 4:
					uni.setClipboardData({
						data:shareInfo.href,
						complete() {
							uni.showToast({
								title: "已复制到剪贴板"
							})
						}
					})
					break;
				case 5:
					plus.share.sendWithSystem({
						type:"web",
						title:shareInfo.title||"",
						thumbs:[shareInfo.imgUrl||""],
						href:shareInfo.href||"",
						content: shareInfo.desc||"",
					})
					break;
			};
		});
		return shareObj;
	}
}

export const commons = {
	constructor(vue = null) {
		this.vue = vue; // 打开后的回调
	},
	getConfig(name){
		let config  = uni.getStorageSync('xinliConfig');
		let res = '';
		if( name == 'checkData'){
			res = config.checkData || '请邀请好友或者购买积分，在我的商场查看更多详情~.';
		}else if( name == 'integralMin'){
			res =  config.integralMin || 40
		}else if( name == 'fenxiangTitle'){
			res = config.fenxiangTitle || '如果您觉得不错,请分享到其他QQ群~';
		}else if( name == 'integralTitle'){
			res = '您目前'+uni.getStorageSync('userInfo').integral+'积分,请购买积分，满' +  this.getConfig('integralMin') +'积分可求助一次！';
		}
		return res;
	},
	async help(type){
		// 用户信息
		const that = this;
		let userInfo = uni.getStorageSync('userInfo');
		
		userInfo.type = type;
		let array = []; 
		userInfo.yonghuBiaoQian.forEach(item => {
			array.push(item.labelId);
		})     
		console.log( userInfo , 'yonghuBiaoQian',array );
		let data = {
			'type' : type,
			'userId' : userInfo.userId,
			'yonghuBiaoQian' : array 
		}
		
		if( type == 2  ){
		// 校验权限
			let checkData = this.getConfig('checkData'); // uni.getStorageSync('xinliConfig').checkData || '请邀请好友或者购买积分，在我的商场查看更多详情~';
		//  
			let jifen = await apiW.getFrequency(userInfo);
			console.log(jifen.data);
			if( !jifen.data ){
				uni.showModal({
					title: this.getConfig('integralTitle'), 
					content: checkData,
					icon: "none",
					cancelText : '取消',
					confirmText : '购买积分',
					success: function (res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/help/zhifu'
							})
						} else if (res.cancel) {
							share.shareUrl();
						}
					}
				})
				return;
			}
		}
		
		// 匹配.           
		let toUserInfo = userSeekHelp(data).then( (res) => {
			store.commit('createConversationActive', res.userId);
			uni.setStorageSync('toUser',res.data);
			console.log('tools',res.data);
			//
			if(type == 2){
				
				// 
				var pages = getCurrentPages();
				var page = pages[pages.length - 1];
				console.log(page);
				var help = '/pages/help/help';
				if( page.route != help ){
					uni.navigateTo({
						url: help
					})
				}else{
					uni.redirectTo({
						url: help
					});
				}
			}else{
				uni.navigateTo({
					url: `/pages/my/userInfo?goRoom=true&userId=${res.data.userId}`
				});
			}
 
		} );
		// 聊天.
	},
	setPage(data){
		uni.setStorageSync('page',data);
	},
	getPage(){
		let data = uni.getStorageSync('page');;
		uni.removeStorageSync("page")
		return data;
	}
}
export const page = {
	userInfo(toUserId,isRoom){
		if( isRoom ){
			uni.navigateTo({
				url: `/pages/my/userInfo?goRoom=true&userId=${toUserId}`
			})
		}else{
			uni.navigateTo({
				url: `/pages/my/userInfo?userId=${toUserId}`
			})
		}
	}
}
export const uniApp = {
	navigateTo(pages){
		uni.navigateTo({
			url: pages
		})
	}
}
export const arrayTool = {
	/** 提取数组对象中的key，重新组装成数组。
	 * @param {Object} array
	 * @param {Object} key
	 * @param {Object} index
	 */
	drawArray(array,key,index){
		let newArray = [];
		// 默认0
		if( !index ){
			index = 0;
		}
		if( !key ){
			key = 'id';
		}
		
		for (var i = index; i < array.length; i++) {
			newArray.push(array[i][key]);
		}
		return newArray;
	},
	/**text是否在数组对象中存在
	 * @param {Object} array 
	 * @param {Object} text 内容
	 * @param {Object} key  对应的key
	 * @param {Object} index 从下标开始
	 */
	includes(array,text,key,index){
		// 默认0
		if( !index ){
			index = 0;
		}
		if( !key ){
			key = 'id';
		}
		
		for (var i = index; i < array.length; i++) {
			if( array[i][key] == text ){
				return true;
			}
		}
		return false;
	}
}


export default {
	share,
	commons,
	page,
	uniApp,
	arrayTool
}
//复制分享链接
			